package cn.renxuan.service.weather;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import cn.renxuan.CommonUtil;

public class WeatherHelper {
    // private static String basepath;
    // private static String realapiurl;
    // private static String apikey;
    // private static String apisecret;
    // private static String weatherDriverName;

    private static Properties servicProperties = null;

    private static List<WeatherConfig> weatherConfigList = new ArrayList<WeatherConfig>();

    public static void registerWeatherConfig() throws Exception {
        String profileName = System.getenv("SPRING_PROFILES_ACTIVE");
        String jdbcConfigFileName = String.format("service%s.properties",
                profileName == null || profileName.isBlank() ? "" : "-" + profileName);

        CommonUtil.printThreadLog("开始加载天气接口配置文件：" + jdbcConfigFileName);
        // 如果没有配置文件，则使用默认的jdbc.properties

        // 读取本地数据库配置文件
        InputStream rs = WeatherHelper.class.getClassLoader().getResourceAsStream(jdbcConfigFileName);
        // 本地文件流加载到Properties对象中
        Properties ps = new Properties();
        ps.load(rs);

        servicProperties = ps;

        // 读取配置jdbc配置文件中的
        // basepath = ps.getProperty("realtimeweather-basepath");
        // realapiurl = ps.getProperty("realtimeweather-realapiurl");
        // apikey = ps.getProperty("realtimeweather-apikey");
        // apisecret = ps.getProperty("realtimeweather-apisecret");
        // weatherDriverName = ps.getProperty("realtimeweather-driverName");
        WeatherConfig lunDearConfig = new WeatherConfig();
        lunDearConfig.setConfigKey(ps.getProperty("realtimeweather-configKey"));
        lunDearConfig.setBasepath(ps.getProperty("realtimeweather-basepath"));
        lunDearConfig.setRealapiurl(ps.getProperty("realtimeweather-realapiurl"));
        lunDearConfig.setApikey(ps.getProperty("realtimeweather-apikey"));
        lunDearConfig.setApisecret(ps.getProperty("realtimeweather-apisecret"));
        lunDearConfig.setDriverName(ps.getProperty("realtimeweather-driverName"));

        // 添加lundear配置
        if (weatherConfigList.stream().filter(c -> lunDearConfig.getConfigKey().equals(c.getConfigKey()))
                .count() <= 0) {
            weatherConfigList.add(lunDearConfig);
        }

        WeatherConfig baiduConfig = new WeatherConfig();
        baiduConfig.setConfigKey(ps.getProperty("baidu-map-configKey"));
        baiduConfig.setBasepath(ps.getProperty("baidu-map-basepath"));
        baiduConfig.setRealapiurl(ps.getProperty("baidu-map-weatherapiurl"));
        baiduConfig.setApiCode(ps.getProperty("baidu-map-ak"));
        baiduConfig.setDriverName(ps.getProperty("baidu-map-driverName"));

        // 添加百度地图配置
        if (weatherConfigList.stream().filter(c -> baiduConfig.getConfigKey().equals(c.getConfigKey()))
                .count() <= 0) {
            weatherConfigList.add(baiduConfig);
        }
    }

    /*
     * 
     */
    public static IWeatherService getWeatherService(String weatherDriverName) throws ClassNotFoundException {
        IWeatherService weather = null;
        if (weatherDriverName != null && !weatherDriverName.isBlank()) {
            Class weatherClass = Class.forName(weatherDriverName);
            try {
                weather = (IWeatherService) weatherClass.getDeclaredConstructor().newInstance();
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                CommonUtil.printThreadLog(String.format("反射天气服务对象失败：%s", e.getMessage()));
            }
        }
        return weather;
    }

    public static String getPropertyVal(String configPropertyName) {
        if (servicProperties == null) {
            try {
                registerWeatherConfig();
            } catch (Exception e) {
                CommonUtil.printThreadLog("加载服务端配置文件失败：" + e.getMessage());
            }
        }
        if (servicProperties != null && configPropertyName != null && !configPropertyName.isBlank()) {
            return servicProperties.getProperty(configPropertyName);
        }
        return null;
    }

    public static WeatherConfig getWeatherConfigByPropName(String configPropertyName) {
        String propertyVal = null;
        if (servicProperties == null) {
            try {
                registerWeatherConfig();
            } catch (Exception e) {
                CommonUtil.printThreadLog("加载服务端配置文件失败：" + e.getMessage());
            }
        }
        if (servicProperties != null && configPropertyName != null && !configPropertyName.isBlank()) {
            propertyVal = servicProperties.getProperty(configPropertyName);
        }
        return getWeatherConfigByKeyVal(propertyVal);
    }

    public static WeatherConfig getWeatherConfigByKeyVal(String configKeyValue) {
        if (configKeyValue == null || configKeyValue.isBlank()) {
            return null;
        }
        WeatherConfig config = weatherConfigList.stream().filter(c -> configKeyValue.equals(c.getConfigKey()))
                .findFirst().orElse(null);
        // config.setApikey(apikey);
        // config.setApisecret(apisecret);
        // config.setBasepath(basepath);
        // config.setRealapiurl(realapiurl);
        return config;
    }
}
